package com.zizy.healthexpert.Activity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.zizy.healthexpert.Config.Config;
import com.zizy.healthexpert.Database.MyDatabase_PersonalBasicInf;
import com.zizy.healthexpert.R;

import java.util.ArrayList;

public class SettingActivity extends ActionBarActivity {
    static int positionLabour=0;/*selected item labour*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        Spinner spinner = (Spinner) findViewById(R.id.spnLabour);
        ArrayAdapter arrayAdapter=new ArrayAdapter(this,R.layout.spinner_item,new ArrayList<String>(){
            {
                this.add("極輕勞動");
                this.add("輕勞動");
                this.add("中勞動");
                this.add("重勞動");
            }
        });

        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(SettingActivity.this,position+"",Toast.LENGTH_SHORT).show();
                positionLabour = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

     }

    public void btnOkAddBasicInformationClicked(View v){
        int age=Config.PersonalInf.age;
        int gender=Config.PersonalInf.gender;
        int labour=Config.PersonalInf.tag;
        TextView txtAge = (TextView) findViewById(R.id.txtAge);
        RadioGroup rdgGender = (RadioGroup) findViewById(R.id.rdgGender);
        Spinner spinner = (Spinner) findViewById(R.id.spnLabour);

        age=Integer.parseInt(txtAge.getText().toString());
        if(age<45) {
            Toast.makeText(this,"對不起暫時只支持45歲及以上。",Toast.LENGTH_SHORT).show();
        }
        switch (rdgGender.getCheckedRadioButtonId()){
            case R.id.rdbFemale:
                gender= Config.FEMALE;
                break;
            case R.id.rdbMale:
                gender= Config.MALE;
                break;
            default:
                Toast.makeText(this,"未選擇性別",Toast.LENGTH_LONG).show();
                return;
        }
//        spinner.getAdapter().get;
        labour = positionLabour;
        MyDatabase_PersonalBasicInf myDatabase_personalBasicInf = new MyDatabase_PersonalBasicInf(this);

        if(age>=70 && (labour==Config.HIGH_LEVEL_WORK || labour==Config.MID_LEVEL_WORK)) {
            labour=Config.LOW_LEVEL_WORK;
        }
        if (age>=60 && labour==Config.HIGH_LEVEL_WORK){
            labour=Config.MID_LEVEL_WORK;
        }

        if (myDatabase_personalBasicInf.SearchDatabase("*").size()!=0){
            myDatabase_personalBasicInf.updateDatabase(age,gender,labour);
        }else{
            myDatabase_personalBasicInf.insertDatabase(age,gender,labour);
        }
        Toast.makeText(this, "記錄成功", Toast.LENGTH_SHORT).show();
        this.finish();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.setting, menu);
        return true;
    }
    public void btnSettingDiseaseClicked(View v){
        startActivity(new Intent(SettingActivity.this,SettingDiseaseActivity.class));
        finish();//TODO
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
